home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Sample Code / AOCE Sample Code / PowerTalk Access Modules / Sample SMSAM / SampleSMSAM Source / BuildingBlocks / FileSpec.cp < prev    next >
Encoding:
Text File  |  1995-07-28  |  15.7 KB  |  696 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        FileSpec.cp
  3.  
  4.     Copyright:    © 1991-1994 by Apple Computer, Inc.
  5.                 All rights reserved.
  6.  
  7.     Part of the AOCE Sample SMSAM Package.  Consult the license
  8.     which came with this software for your specific legal rights.
  9.  
  10. */
  11.  
  12.  
  13.  
  14. #ifndef    __FILESPEC__
  15. #include "FileSpec.h"
  16. #endif
  17.  
  18. #ifndef    __ERRORS__
  19. #include "Errors.h"
  20. #endif
  21.  
  22. #ifndef    __TOOLUTILS__
  23. #include "ToolUtils.h"
  24. #endif
  25.  
  26. #ifndef    __OSUTILS__
  27. #include "OSUtils.h"
  28. #endif
  29.  
  30. #ifndef    __DEBUGASSERT__
  31. #include "DebugAssert.h"
  32. #endif
  33.  
  34. #if defined ( BovineServer )
  35.  
  36. #ifndef    __DEBUGGINGGEAR__
  37. #include "DebuggingGear.h"
  38. #endif
  39.  
  40. #endif
  41.  
  42. #pragma segment FileSpec
  43.  
  44. /***********************************|****************************************/
  45.  
  46. static void
  47. MakeString ( const char* a, StringPtr b, unsigned long blen )
  48. {
  49.     StringPtr c = b; 
  50.     register unsigned char stringLen = 0;
  51.  
  52.     while ( ( blen-- > 0 ) && ( *a ) )
  53.     {    *++b = *a++;
  54.         stringLen ++;
  55.     }
  56.     
  57.     c [ 0 ] = stringLen;
  58. }
  59.  
  60. /***********************************|****************************************/
  61.  
  62. static void
  63. MakeString ( const StringPtr a, char* b, unsigned long blen )
  64. {
  65.     unsigned char l = *a;
  66.     StringPtr c = (StringPtr) a;
  67.  
  68.     while ( ( blen-- > 0 ) && ( l-- > 0 ) )
  69.         *b++ = *c++;
  70.  
  71.     *b = 0;
  72. }
  73.  
  74. /***********************************|****************************************/
  75.  
  76. static void
  77. MakeString ( const StringPtr a, StringPtr b, unsigned long blen )
  78. {
  79.     unsigned short l = a [ 0 ];
  80.     StringPtr c = (StringPtr) a;
  81.  
  82.     ASSERT_RETURN ( l <= blen );
  83.  
  84.     do
  85.         *b++ = *c++;
  86.     while ( l-- > 0 );
  87. }
  88.  
  89. /***********************************|****************************************/
  90. /***********************************|****************************************/
  91.  
  92. TFileSpec::TFileSpec ():
  93.     THFSSpec ()
  94. {
  95. }
  96.  
  97. /***********************************|****************************************/
  98.  
  99. TFileSpec::~TFileSpec ()
  100. {
  101. }
  102.  
  103. /***********************************|****************************************/
  104. /***********************************|****************************************/
  105.  
  106. TFolderSpec::TFolderSpec ():
  107.     THFSSpec ()
  108. {
  109. }
  110.  
  111. /***********************************|****************************************/
  112.  
  113. TFolderSpec::~TFolderSpec ()
  114. {
  115. }
  116.  
  117. /***********************************|****************************************/
  118. /***********************************|****************************************/
  119.  
  120. THFSSpec::THFSSpec ():
  121.     fCachedPath ( nil )
  122. {
  123.     FSSpec& spec = GetSpec ();
  124.     spec.vRefNum = 0;
  125.     spec.parID = 0;
  126.     spec.name [ 0 ] = 0;
  127. }
  128.  
  129. /***********************************|****************************************/
  130.  
  131. THFSSpec::THFSSpec ( const char* file ):
  132.     fCachedPath ( nil )
  133. {
  134.     FSSpec& spec = GetSpec ();
  135.     MakeString ( file, spec.name, sizeof ( spec.name ) );
  136.     SetToCurrentDirectory ();
  137.  
  138. //     if ( callMakeFSSpec )
  139. //         ::FSMakeFSSpec ( spec.vRefNum, spec.parID, spec.name, &spec );
  140. }
  141.  
  142. /***********************************|****************************************/
  143.  
  144. THFSSpec::THFSSpec ( const StringPtr file ):
  145.     fCachedPath ( nil )
  146. {
  147.     SetToCurrentDirectory ();
  148.     MakeString ( file, GetSpec ().name, sizeof ( GetSpec ().name ) );
  149. }
  150.  
  151. /***********************************|****************************************/
  152.  
  153. void
  154. THFSSpec::SetToCurrentDirectory ()
  155. {
  156.     FSSpec& spec = GetSpec ();
  157.     WDPBRec pb;
  158.     pb.ioNamePtr = nil;
  159.  
  160.     if ( noErr == PBHGetVol ( &pb, false ) )
  161.     {
  162.         spec.vRefNum = pb.ioWDVRefNum;
  163.         spec.parID = pb.ioWDDirID;
  164.     }
  165.     else
  166.     {
  167.         spec.vRefNum = -1;    // default to first drive
  168.         spec.parID = 2;        // default to root directory
  169.     }
  170. }
  171.  
  172. /***********************************|****************************************/
  173.  
  174. THFSSpec::THFSSpec ( short vRefNum, long parID, const StringPtr name ):
  175.     fCachedPath ( nil )
  176. {
  177.     FSSpec& spec = GetSpec ();
  178.     spec.vRefNum = vRefNum;
  179.     spec.parID = parID;
  180.     MakeString ( name, spec.name, sizeof ( spec.name ) );
  181. }
  182.  
  183. /***********************************|****************************************/
  184.  
  185. THFSSpec::THFSSpec ( short vRefNum, long parID, const char* name ):
  186.     fCachedPath ( nil )
  187. {
  188.     FSSpec& spec = GetSpec ();
  189.     spec.vRefNum = vRefNum;
  190.     spec.parID = parID;
  191.     MakeString ( name, spec.name, sizeof ( spec.name ) );
  192. }
  193.  
  194. /***********************************|****************************************/
  195.  
  196. THFSSpec::THFSSpec ( const FSSpec& spec ):
  197.     fCachedPath ( nil )
  198. {
  199.     GetSpec () = spec;
  200. }
  201.  
  202. /***********************************|****************************************/
  203.  
  204. THFSSpec::THFSSpec ( const THFSSpec& spec ):
  205.     fCachedPath ( nil )
  206. {
  207.     GetSpec () = spec.GetSpec ();
  208. }
  209.  
  210. /***********************************|****************************************/
  211.  
  212. THFSSpec::~THFSSpec ()
  213. {
  214.     delete fCachedPath;
  215. }
  216.  
  217. /***********************************|****************************************/
  218.  
  219. THFSSpec&
  220. THFSSpec::operator = ( const THFSSpec& that )
  221. {
  222.     if ( this != &that )
  223.     {
  224.         delete fCachedPath;
  225.         fCachedPath = nil;
  226.         GetSpec () = that.GetSpec ();
  227.     }
  228.  
  229.     return *this;
  230. }
  231.  
  232. /***********************************|****************************************/
  233.  
  234. Boolean
  235. THFSSpec::operator == ( const THFSSpec& that ) const
  236. {
  237.     const FSSpec& thisSpec = GetSpec ();
  238.     const FSSpec& thatSpec = that.GetSpec ();
  239.  
  240.     return
  241.         ::EqualString ( thisSpec.name, thatSpec.name, false, true )  &&
  242.         ( thisSpec.parID == thatSpec.parID ) &&
  243.         ( thisSpec.vRefNum == thatSpec.vRefNum );
  244. }
  245.  
  246. /***********************************|****************************************/
  247.  
  248. unsigned long
  249. THFSSpec::GetPathLength () const
  250. {
  251.     return ::strlen ( GetPath () );
  252. }
  253.  
  254. /***********************************|****************************************/
  255.  
  256. const char*
  257. THFSSpec::GetCName () const
  258. {
  259.     FSSpec& spec = ( (THFSSpec*) this )->GetSpec ();
  260.     spec.name [ spec.name [ 0 ] + 1 ] = 0;
  261.     return (const char*) &spec.name [ 1 ];
  262. }
  263.  
  264. /***********************************|****************************************/
  265.  
  266. void
  267. THFSSpec::GetName ( Str63 string ) const
  268. {
  269.     const StringPtr name = GetSpec ().name;
  270.     ::BlockMove ( name, string, name [ 0 ] );
  271. }
  272.  
  273. /***********************************|****************************************/
  274.  
  275. void
  276. THFSSpec::GetName ( char* buffer, unsigned long size ) const
  277. {
  278.     ::strncpy ( buffer, GetCName (), (unsigned int) size );
  279. }
  280.  
  281. /***********************************|****************************************/
  282.  
  283. void
  284. THFSSpec::GetParentName ( char* buffer, unsigned long size ) const
  285. {
  286.     ::strncpy ( buffer, "not implemented", (unsigned int) size );
  287. }
  288.  
  289. /***********************************|****************************************/
  290.  
  291. void
  292. THFSSpec::GetVolumeName ( char* buffer, unsigned long size ) const
  293. {
  294.     ::strncpy ( buffer, "not implemented", (unsigned int) size );
  295. }
  296.  
  297. /***********************************|****************************************/
  298.  
  299. const char*
  300. THFSSpec::GetPath () const
  301. {
  302.     if ( !fCachedPath )
  303.         ( (THFSSpec*) this )->fCachedPath = CreatePath ();
  304.  
  305.     return fCachedPath;
  306. }
  307.  
  308. /***********************************|****************************************/
  309.  
  310. char*
  311. THFSSpec::CreatePath () const
  312. {
  313.     const char* name = GetCName ();
  314.     char* path = new char [ strlen ( name ) + 1 ];
  315.  
  316.     if ( path )
  317.         ::strcpy ( path, name );
  318.  
  319.     return path;
  320. }
  321.  
  322. /***********************************|****************************************/
  323.  
  324. #if debug
  325.  
  326. Boolean
  327. THFSSpec::InvariantCheck ( ostream& /* stream */ ) const
  328. {
  329.     return true;
  330. }
  331.  
  332. ostream&
  333. THFSSpec::operator >> ( ostream& stream ) const
  334. {
  335.     return stream << "THFSSpec @ " << (void*) this << ", vRefNum: " << GetSpec ().vRefNum << ", parID: " << GetSpec ().parID << ", name: \"" << (const char*) &GetSpec ().name [ 1 ] << "\"";
  336. }
  337.  
  338. #endif
  339.  
  340. /***********************************|****************************************/
  341. /***********************************|****************************************/
  342.  
  343. TFileSpec::TFileSpec ( const char* string ):
  344.     THFSSpec ( string )
  345. {
  346. }
  347.  
  348. /***********************************|****************************************/
  349.  
  350. TFileSpec::TFileSpec ( short vRefNum, long parentID, const char* name ):
  351.     THFSSpec ( vRefNum, parentID, name )
  352. {
  353. }
  354.  
  355. /***********************************|****************************************/
  356.  
  357. TFileSpec::TFileSpec ( short vRefNum, long parentID, const StringPtr name ):
  358.     THFSSpec ( vRefNum, parentID, name )
  359. {
  360. }
  361.  
  362. /***********************************|****************************************/
  363.  
  364. TFileSpec::TFileSpec ( const THFSSpec& that ):
  365.     THFSSpec ( that )
  366. {
  367. }
  368.  
  369. /***********************************|****************************************/
  370.  
  371. TFileSpec::TFileSpec ( const FSSpec& fsspec ):
  372.     THFSSpec ( fsspec )
  373. {
  374. }
  375.  
  376. /***********************************|****************************************/
  377.  
  378. TFileSpec::TFileSpec ( const TFileSpec& that ):
  379.     THFSSpec ( that )
  380. {
  381. }
  382.  
  383. /***********************************|****************************************/
  384.  
  385. Boolean
  386. TFileSpec::DoesFileExist () const
  387. {
  388.     FileParam filePB;
  389.  
  390.     const FSSpec& spec = GetSpec ();
  391.  
  392.     filePB.ioNamePtr = spec.name;
  393.     filePB.ioVRefNum = spec.vRefNum;
  394.     filePB.ioFlNum = spec.parID;
  395.     filePB.ioFDirIndex = 0;
  396.  
  397.     return PBHGetFInfo ( (HParamBlockRec*) &filePB, false ) == noErr;
  398. }
  399.  
  400. /***********************************|****************************************/
  401.  
  402. Boolean
  403. TFileSpec::CreateFile ( OSType creator, OSType type )
  404. {
  405.     OSErr error = ::FSpCreate ( *this, creator, type, 0 );
  406.     return ( error == noErr ) || ( error == dupFNErr );
  407. }
  408.  
  409. /***********************************|****************************************/
  410.  
  411. Boolean
  412. TFileSpec::DeleteFile ()
  413. {
  414.     OSErr error = ::FSpDelete ( *this );
  415.     return ( error == noErr ) || ( error == fnfErr );
  416. }
  417.  
  418. /***********************************|****************************************/
  419.  
  420. Boolean
  421. TFileSpec::GetLogicalSize ( unsigned long& logicalSize ) const
  422. {
  423.     FileParam filePB;
  424.     filePB.ioNamePtr = (StringPtr) GetPName ();
  425.     filePB.ioVRefNum = GetVolume ();
  426.     filePB.ioFlNum = GetParent ();
  427.     filePB.ioFDirIndex = 0;
  428.  
  429.     if ( noErr == PBHGetFInfo ( (HParamBlockRec*) &filePB, false ) )
  430.     {
  431.         logicalSize = filePB.ioFlLgLen;
  432.         return true;
  433.     }
  434.     else
  435.     {
  436.         logicalSize = 0;
  437.         return false;
  438.     }
  439. }
  440.  
  441. /***********************************|****************************************/
  442.  
  443. Boolean
  444. TFileSpec::GetCreationDate ( unsigned long& creationDate ) const
  445. {
  446.     FileParam filePB;
  447.     filePB.ioNamePtr = (StringPtr) GetPName ();
  448.     filePB.ioVRefNum = GetVolume ();
  449.     filePB.ioFlNum = GetParent ();
  450.     filePB.ioFDirIndex = 0;
  451.  
  452.     if ( noErr == PBHGetFInfo ( (HParamBlockRec*) &filePB, false ) )
  453.     {
  454.         creationDate = filePB.ioFlCrDat;
  455.         return true;
  456.     }
  457.     else
  458.     {
  459.         creationDate = 0;
  460.         return false;
  461.     }
  462. }
  463.  
  464. /***********************************|****************************************/
  465.  
  466. Boolean
  467. TFileSpec::GetModificationDate ( unsigned long& modificationDate ) const
  468. {
  469.     FileParam filePB;
  470.     filePB.ioNamePtr = (StringPtr) GetPName ();
  471.     filePB.ioVRefNum = GetVolume ();
  472.     filePB.ioFlNum = GetParent ();
  473.     filePB.ioFDirIndex = 0;
  474.  
  475.     if ( noErr == PBHGetFInfo ( (HParamBlockRec*) &filePB, false ) )
  476.     {
  477.         modificationDate = filePB.ioFlMdDat;
  478.         return true;
  479.     }
  480.     else
  481.     {
  482.         modificationDate = 0;
  483.         return false;
  484.     }
  485. }
  486.  
  487. /***********************************|****************************************/
  488. /***********************************|****************************************/
  489.  
  490. TFolderSpec::TFolderSpec ( const char* string ):
  491.     THFSSpec ( string ),
  492.     fID ( 0 )
  493. {
  494. }
  495.  
  496. /***********************************|****************************************/
  497.  
  498. TFolderSpec::TFolderSpec ( short vRefNum, long parentID, const char* name ):
  499.     THFSSpec ( vRefNum, parentID, name ),
  500.     fID ( 0 )
  501. {
  502. }
  503.  
  504. /***********************************|****************************************/
  505.  
  506. TFolderSpec::TFolderSpec ( short vRefNum, long parentID, const StringPtr name ):
  507.     THFSSpec ( vRefNum, parentID, name ),
  508.     fID ( 0 )
  509. {
  510. }
  511.  
  512. /***********************************|****************************************/
  513.  
  514. TFolderSpec::TFolderSpec ( const THFSSpec& that ):
  515.     THFSSpec ( that ),
  516.     fID ( 0 )
  517. {
  518. }
  519.  
  520. /***********************************|****************************************/
  521.  
  522. TFolderSpec::TFolderSpec ( const TFolderSpec& that ):
  523.     THFSSpec ( that ),
  524.     fID ( that.fID )
  525. {
  526. }
  527.  
  528. /***********************************|****************************************/
  529.  
  530. Boolean
  531. TFolderSpec::CreateFolder ()
  532. {
  533.     long refNum = 0;
  534.     OSErr error = ::FSpDirCreate ( *this, 0, &refNum );
  535.     return ( error == noErr ) || ( error == dupFNErr );
  536. }
  537.  
  538. /***********************************|****************************************/
  539.  
  540. Boolean
  541. TFolderSpec::DeleteFolder ()
  542. {
  543.     long refNum = 0;
  544.     OSErr error = ::FSpDelete ( *this );
  545.     return ( error == noErr ) || ( error == fnfErr );
  546. }
  547.  
  548. /***********************************|****************************************/
  549.  
  550. long
  551. TFolderSpec::GetID () const
  552. {
  553.     if ( fID )
  554.         return fID;
  555.     else
  556.         return ( (TFolderSpec*) this )->fID = LookupID ();
  557. }
  558.  
  559. /***********************************|****************************************/
  560.  
  561. long
  562. TFolderSpec::LookupID () const
  563. {
  564.     CInfoPBRec paramBlock;
  565.     paramBlock.dirInfo.ioVRefNum = GetVolume ();
  566.     paramBlock.dirInfo.ioDrDirID = GetParent ();
  567.     paramBlock.dirInfo.ioFDirIndex = 0;
  568.     paramBlock.dirInfo.ioNamePtr = (StringPtr) GetPName ();
  569.     return PBGetCatInfo ( ¶mBlock, false ) == noErr ? paramBlock.dirInfo.ioDrDirID : 0;
  570. }
  571.  
  572. /***********************************|****************************************/
  573.  
  574. Boolean
  575. TFolderSpec::DoesFolderExist () const
  576. {
  577.     return false;
  578. }
  579.  
  580. /***********************************|****************************************/
  581.  
  582. unsigned long
  583. TFolderSpec::CountChildrenFiles () const
  584. {
  585.     DirInfo    dirInfoPB;
  586.     Str32 dirName;
  587.     dirName [ 0 ] = 0;
  588.  
  589.     dirInfoPB.ioNamePtr = (StringPtr) dirName;
  590.     dirInfoPB.ioVRefNum = GetVolume ();
  591.     dirInfoPB.ioFDirIndex = -1;
  592.     dirInfoPB.ioDrDirID = GetID ();
  593.  
  594.     return PBGetCatInfo ( (CInfoPBRec*) &dirInfoPB, false ) == noErr ? dirInfoPB.ioDrNmFls : 0;
  595. }
  596.  
  597. /***********************************|****************************************/
  598.  
  599. Boolean
  600. TFolderSpec::GetChildFile ( unsigned long index, TFileSpec& file ) const
  601. {
  602.     CInfoPBRec paramBlock;
  603.  
  604.     paramBlock.dirInfo.ioVRefNum = file.GetSpec ().vRefNum = GetVolume ();
  605.     paramBlock.dirInfo.ioDrDirID = file.GetSpec ().parID = GetID ();
  606.     paramBlock.dirInfo.ioFDirIndex = (short) index;
  607.     paramBlock.dirInfo.ioNamePtr = file.GetSpec ().name;
  608.     paramBlock.dirInfo.ioNamePtr [ 0 ] = 0;
  609.  
  610.     if ( PBGetCatInfo ( ¶mBlock, false ) == noErr )
  611.         if ( !BitAnd ( paramBlock.dirInfo.ioDrUsrWds.frFlags, fInvisible ) )
  612.             if ( !BitTst ( ¶mBlock.dirInfo.ioFlAttrib, 3 ) )
  613.                 return true;
  614.  
  615.     return false;
  616. }
  617.  
  618. /***********************************|****************************************/
  619.  
  620. #if debug
  621.  
  622. ostream&
  623. TFolderSpec::operator >> ( ostream& stream ) const
  624. {
  625.     stream << "TFolderSpec @ " << (void*) this << ", dirID " << GetID () << "\n";
  626.     return THFSSpec::operator >> ( stream );
  627. }
  628.  
  629. #endif
  630.  
  631. /***********************************|****************************************/
  632. /***********************************|****************************************/
  633.  
  634. TFolderFileIterator::TFolderFileIterator ( const TFolderSpec& folder ):
  635.     fCurrent ( nil ),
  636.     fVRefNum ( folder.GetVolume () ),
  637.     fParID ( folder.GetID () )
  638. {
  639.     fParams.dirInfo.ioVRefNum = fVRefNum;
  640.     fParams.dirInfo.ioDrDirID = fParID;
  641.     fParams.dirInfo.ioFDirIndex = 0;
  642.     fParams.dirInfo.ioNamePtr = fName;
  643.     fName [ 0 ] = 0;
  644. }
  645.  
  646. /***********************************|****************************************/
  647.  
  648. TFolderFileIterator::~TFolderFileIterator ()
  649. {
  650.     delete fCurrent;
  651. }
  652.  
  653. /***********************************|****************************************/
  654.  
  655. const TFileSpec*
  656. TFolderFileIterator::FirstFile ()
  657. {
  658.     fParams.dirInfo.ioVRefNum = fVRefNum;
  659.     fParams.dirInfo.ioDrDirID = fParID;
  660.     fParams.dirInfo.ioFDirIndex = 0;
  661.     return NextFile ();
  662. }
  663.  
  664. /***********************************|****************************************/
  665.  
  666. const TFileSpec*
  667. TFolderFileIterator::NextFile ()
  668. {
  669.     do
  670.     {
  671.         fParams.dirInfo.ioVRefNum = fVRefNum;
  672.         fParams.dirInfo.ioDrDirID = fParID;
  673.         ++fParams.dirInfo.ioFDirIndex;
  674.  
  675.         if ( PBGetCatInfo ( &fParams, false ) == noErr )
  676.         {
  677.             if ( BitAnd ( fParams.dirInfo.ioDrUsrWds.frFlags, fInvisible ) == 0 )
  678.             {
  679.                 if ( !BitTst ( &fParams.hFileInfo.ioFlAttrib, 3 ) )
  680.                 {
  681.                     delete fCurrent;
  682.                     return fCurrent = new TFileSpec ( fParams.dirInfo.ioVRefNum, fParams.hFileInfo.ioFlParID, fParams.dirInfo.ioNamePtr );
  683.                 }
  684.             }
  685.         }
  686.         else
  687.         {
  688.             return nil;
  689.         }
  690.     }
  691.     while ( true );
  692. }
  693.  
  694. /***********************************|****************************************/
  695.  
  696.